package com.dharma.mydiet.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DAORegister {
	public final static String KEY_ID = "idRegister";
	public final static String KEY_NAME = "name";
	public final static String KEY_DESCRIPTION = "description";
	public final static String DATABASE_TABLE = "register";
	SQLiteDatabase myDataBase;

	private static final String[] cols = new String[] { KEY_ID, KEY_NAME, KEY_DESCRIPTION };

	private static DAORegister instance;

	public static DAORegister getInstance (){
		if (instance==null){
			instance=new DAORegister();
		}
		return instance;
	}
	public DAORegister() {
		myDataBase = DBManager.getInstance().getMyDataBase();
	}

	public void insertRegister(TransactionalRegister register) {
		ContentValues newValues = new ContentValues();
		//newValues.put(KEY_ID, register.getIdRegister());
		newValues.put(KEY_NAME, register.getName());
		newValues.put(KEY_DESCRIPTION, register. getDescription());
		myDataBase.beginTransaction();
		myDataBase.insert(DATABASE_TABLE, null, newValues);
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
	}

	public boolean removeRegister(TransactionalRegister register) {

		boolean ret = false;
		int row = this.getRow(String.valueOf(register.getIdRegister()));
		if (row >= 0) {
			ret = removeRegister(row);
		}
		return ret;
	}

	private int getRow(String register) {
		int row = -1;
		try {
			Cursor result = null;
			result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "='" + register + "'", null, null,
					null, null, null);

			if (!((result.getCount() == 0) || !result.moveToFirst())) {
				if (result.moveToFirst()) {
					row = result.getInt(result.getColumnIndex(KEY_ID));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	public boolean removeRegister(long _rowIndex) {
		myDataBase.beginTransaction();
		boolean result = myDataBase.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
		return result;

	}

	public boolean updateRegister(TransactionalRegister register) {
		int row = this.getRow(String.valueOf(register.getDescription()));
		return updateRegister(row, register);
	}

	public boolean updateRegister(Integer _rowIndex, TransactionalRegister register) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_ID, register.getIdRegister());
		newValues.put(KEY_NAME, register.getName());
		newValues.put(KEY_DESCRIPTION, register.getDescription());
		return myDataBase.update(DATABASE_TABLE, newValues, KEY_ID + "=" + _rowIndex, null) > 0;
	}

	public TransactionalRegister getRegister(String registerID) {
		TransactionalRegister register = null;
		Cursor result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "= '" + registerID + "'", null, null, null,
				null, null);
		if (!((result.getCount() == 0) || !result.moveToFirst())) {
			if (result.moveToFirst()) {
				register = new TransactionalRegister(result.getInt(result.getColumnIndex(KEY_ID)), result.getString(result
						.getColumnIndex(KEY_NAME)), result.getString(result.getColumnIndex(KEY_DESCRIPTION)));
			}
		}
		return register;
	}

	public ArrayList<TransactionalRegister> getRegisters() {
		ArrayList<TransactionalRegister> registers = new ArrayList<TransactionalRegister>();
		Cursor result = myDataBase.query(DATABASE_TABLE, cols, null, null, null, null, KEY_ID);
		if (result.moveToFirst())
			do {
				registers.add(new TransactionalRegister(result.getInt(result.getColumnIndex(KEY_ID)), result.getString(result
						.getColumnIndex(KEY_NAME)), result.getString(result.getColumnIndex(KEY_DESCRIPTION))));
			} while (result.moveToNext());
		return registers;
	}

	
	public static void create(TransactionalRegister register) {

	}

	public static TransactionalFood read(int idRegister) {
		return null;

	}

	public static void update(TransactionalRegister register) {

	}

	public static void delete(int idRegister) {

	}
}
